<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Model\ExamCategory;
use App\Model\Exam;
use App\Model\PaperCategory;
use App\Model\Paper;
use App\Model\PaperQuestions;
use App\Model\PaperRecord;
use App\Model\PaperRecordQuestions;
use App\Model\Users;

class ExamController extends Controller
{

	// public function __construct()
    // {
    //     $this->middleware('guest.admin', ['except' => 'category_index']);
    // }
	// $user = Auth::user();
    // 试题分类列表

	public function category_index(Request $request){
		if($request['type'] == 1){
			$res = ExamCategory::where('c_type','1')->where('is_delete','0')->get();
			$data = [
				'code' => 0,
				'msg'   => '请求成功',
				'count' => count($res),
				'data'  => $res
			];
			return response()->json($data);
		}
		
		$query=ExamCategory::where('c_type','1')->where('is_delete','0')->orderBy('c_sort','desc');

		$res=$query->paginate($request->get('limit',10))->toArray();

		foreach($res['data'] as &$v){
			$v['child'] = ExamCategory::where('c_fid',$v['id'])->get();
		}
		
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => $res['total'],
			'data'  => $res['data']
		];
		return response()->json($data);
	}





	// 试题分类添加/修改

	public function category_edit(Request $request){

		if(empty($request['id'])){
			// 编辑
			$category = new ExamCategory();
		}else{
			// 新增
			$category = ExamCategory::findorfail($request['id']);
			if(!$category){
				$data = [
					'code' => 1,
					'msg'   => '数据不存在',
					'count' => 0,
					'data'  => array()
				];
				return response()->json($data);
			}
			
		}
		$category->c_sort = $request['c_sort'];
		$category->c_fid = $request['c_fid'];
		$category->c_type = '1';
		$category->c_name = $request['c_name'];
		$category->save();
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => 0,
			'data'  => array()
		];
		return response()->json($data);
		

	}

	// 试题分类删除

	public function category_del(Request $request){
		if(empty($request['id'])){
			$data = [
				'code' => 1,
				'msg'   => 'id不可为空',
				'count' => 0,
				'data'  => array()
			];
			
			return response()->json($data);
		}
		$category=ExamCategory::findorfail($request['id']);
		$category->is_delete = $request['is_delete'];
		$category->save();
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => 0,
			'data'  => array()
		];
		
		return response()->json($data);
	}


	// 试题列表
	public function index(Request $request){
		$where = array();
		if($request['cid']){
			$where[] = ['cid','=',$request['cid']];
		}

		if($request['title']){
			$where[] = ['exam_content','like','%'.$request['title'].'%'];
		}


		if($where){
			$query = Exam::with(['category'])->where('is_delete','0')->where($where)->orderBy('id','desc');
		}else{
			$query = Exam::with(['category'])->where('is_delete','0')->orderBy('id','desc');
		}
		
		
		
		$res=$query->paginate($request->get('limit',10))->toArray();
		
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => $res['total'],
			'data'  => $res['data']
		];
		return response()->json($data);
	}

	// 试题添加/修改


	// 试题单条数据获取

	public function get_exam_data(Request $request){
		if(empty($request['id'])){
			$data = [
				'code' => 1,
				'msg'   => 'id不可为空',
				'count' => 0,
				'data'  => array()
			];
			
			return response()->json($data);
		}
		
		$exam = Exam::with(['category'])->where('id',$request['id'])->first();
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => '',
			'data'  => $exam
		];
		return response()->json($data);
	}



	public function edit(Request $request){
		if(empty($request['id'])){
			// 编辑
			$exam = new Exam();
		}else{
			// 新增
			$exam = Exam::findorfail($request['id']);
			
		}
		$exam->cid = $request['cid'];
		$exam->type = 1;
		$exam->difficulty = $request['difficulty'];//试题难度:1简单2中等3难4特别难
		$exam->exam_content = $request['exam_content'];//试题内容/题帽
		$exam->answer = $request['answer'];//答案选项
		$exam->analysis = $request['analysis'];//试题解析内容
		$exam->score = $request['score'];//试题总分
		$exam->option1 = $request['option1'];//以下是答案，最多10个
		$exam->option2 = $request['option2'];
		$exam->option3 = $request['option3'];
		$exam->option4 = $request['option4'];
		$exam->option5 = $request['option5'];
		$exam->option6 = $request['option6'];
		$exam->option7 = $request['option7'];
		$exam->option8 = $request['option8'];
		$exam->option9 = $request['option10'];
		$exam->option10 = $request['option10'];
		if(empty($exam->cid)){
			$data = [
				'code' => 0,
				'msg'   => '分类不能为空',
				'count' => 0,
				'data'  => array()
			];
			return response()->json($data);
		}

		$exam->save();
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => 0,
			'data'  => array()
		];
		return response()->json($data);
	}

	// 试题删除
	public function del(Request $request){
		if(empty($request['id'])){
			$data = [
				'code' => 1,
				'msg'   => 'id不可为空',
				'count' => 0,
				'data'  => array()
			];
			
			return response()->json($data);
		}
		$ids = explode(',',$request['id']);
		
		foreach($ids as $v){
			$exam=Exam::findorfail($v);			
			$exam->is_delete = '1';
			$exam->save();
		}
		
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => 0,
			'data'  => array()
		];
		
		return response()->json($data);
	}



	// 试卷分类列表
	public function paper_category_index(Request $request){
		$query=PaperCategory::where('p_type','1')->where('is_delete','0')->orderBy('p_sort','desc');

		$res=$query->paginate($request->get('limit',10))->toArray();

		foreach($res['data'] as &$v){
			$v['child'] = PaperCategory::where('p_fid',$v['id'])->get();
		}
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => $res['total'],
			'data'  => $res['data']
		];
		return response()->json($data);
	}


	// 试卷分类添加/修改
	public function paper_category_edit(Request $request){

		if(empty($request['id'])){
			// 编辑
			$category = new PaperCategory();
		}else{
			// 新增
			$category = PaperCategory::findorfail($request['id']);
			
		}
		
		$category->p_sort = $request['p_sort'];
		$category->p_fid = $request['p_fid'];
		$category->p_type = 1;
		$category->p_name = $request['p_name'];
		$category->save();
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => 0,
			'data'  => array()
		];
		return response()->json($data);
	}

	// 试卷分类删除

	public function paper_category_del(Request $request){
		if(empty($request['id'])){
			$data = [
				'code' => 1,
				'msg'   => 'id不可为空',
				'count' => 0,
				'data'  => array()
			];
			
			return response()->json($data);
		}
		$category = PaperCategory::findorfail($request['id']);
		$category->is_delete = '1';
		$category->save();
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => 0,
			'data'  => array()
		];
		
		return response()->json($data);
	}


	// 试卷列表
	public function paper_index(Request $request){
		$where = [];
		if($request['title']){
			$where[] = ['title','like','%'.$request['title'].'%'];
		}

		if($request['cid']){
			$where[] = ['cid','=',$request['cid']];
		}
		
		if($where){
			// dump($where);exit;
			$query = Paper::with(['category'])->where('is_delete','0')->where($where)->orderBy('id','desc');
		}else{
			$query = Paper::with(['category'])->where('is_delete','0')->orderBy('id','desc');
		}
		
		
		$res=$query->paginate($request->get('limit',10))->toArray();
		
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => $res['total'],
			'data'  => $res['data']
		];
		return response()->json($data);
	}

	// 试卷添加/修改
	public function paper_edit(Request $request){
		if(empty($request)){
			$data = [
				'code' => 1,
				'msg'   => 'id不可为空',
				'count' => 0,
				'data'  => array()
			];
			
			return response()->json($data);
		}
		if(empty($request['id'])){
			// 编辑
			$paper = new Paper();
		}else{
			// 新增
			$paper = Paper::findorfail($request['id']);
			
		}
		$paper->cid = $request['cid'];//分类
		$paper->title = $request['title'];//试卷标题
		$paper->score = $request['score'];//试卷总分
		$paper->pass_score = $request['pass_score'];//及格分数
		$paper->exam_duration = $request['exam_duration'];//考试时长
		$paper->starttime = $request['starttime'];//考试开始时间
		$paper->endtime = $request['endtime'];//考试结束时间
		$paper->exam_num = $request['exam_num'];//考试次数

		$paper->save();
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => 0,
			'data'  => array()
		];
		return response()->json($data);
	}


	

	// 试卷删除
	public function paper_del(Request $request){
		if(empty($request['id'])){
			$data = [
				'code' => 1,
				'msg'   => 'id不可为空',
				'count' => 0,
				'data'  => array()
			];
			
			return response()->json($data);
		}
		$paper = Paper::findorfail($request['id']);
		$paper->is_delete = $request['is_delete'];
		$paper->save();
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => 0,
			'data'  => array()
		];
		
		return response()->json($data);
	}

	// 试卷组卷添加（试卷id及试题的id数组）
	public function questions_add(Request $request){
		if(empty($request['pid'])){
			$data = [
				'code' => 1,
				'msg'   => '试卷id不可为空',
				'count' => 0,
				'data'  => array()
			];
			
			return response()->json($data);
		}else if(empty($request['eid'])){
			$data = [
				'code' => 1,
				'msg'   => '试题id不可为空',
				'count' => 0,
				'data'  => array()
			];
			
			return response()->json($data);
		}

		$eids = explode(',',$request['eid']);
		
		// 删除不存在的试题
		$delresult = PaperQuestions::whereNotIn('eid',$eids)->delete();
		
		foreach($eids as $v){
			
			$res = PaperQuestions::where(['eid'=>$v,'pid'=>$request['pid']])->first();
			
			if(empty($res)){
				$questions = new PaperQuestions();
				$questions->eid = $v;
				$questions->pid = $request['pid'];
				$questions->save();
	
			}
			
		}
		// 更新试卷总分

		$this->update_score($request['pid']);

		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => 0,
			'data'  => array()
		];
		
		return response()->json($data);
	}

	public function update_score($pid){
		$eid_data = PaperQuestions::where('pid',$pid)->select('eid')->get();
		if(!empty($eid_data)){
			$eid_data = $eid_data->toArray();
		}else{
			$eid_data = [];
		}
		
		$eids = array_column($eid_data,'eid');
		
		$score = Exam::whereIn('id',$eids)->sum('score');
		
		$paper = Paper::findorfail($pid);
		$paper->score = $score;
		$paper->save();
	}

	// 试卷组卷内容删除
	public function questions_del(Request $request){
		if(empty($request['pid']) || empty($request['eid'])){
			$data = [
				'code' => 1,
				'msg'   => '试卷id不可为空',
				'count' => 0,
				'data'  => array()
			];
			
			return response()->json($data);
		}
		
		$res = PaperQuestions::where('pid',$request['pid'])->whereIn('eid',explode(',',$request['eid']))->delete();
		// dump($res);exit;
		if($res){
			$this->update_score($request['pid']);
		}
		
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => 0,
			'data'  => array()
		];
		
		return response()->json($data);
	}

	// 组卷列表
	public function questions_index(Request $request){

		if(empty($request['pid'])){
			$data = [
				'code' => 1,
				'msg'   => 'id不可为空',
				'count' => 0,
				'data'  => array()
			];
			
			return response()->json($data);
		}

		$paper_data = Paper::where('id',$request['pid'])->first();
		$eid_data = PaperQuestions::where('pid',$request['pid'])->select('eid')->get();
		if(!empty($eid_data)){
			$eid_data = $eid_data->toArray();
		}else{
			$eid_data = [];
		}
		
		$eids = array_column($eid_data,'eid');
		
		$res = Exam::whereIn('id',$eids)->get();
		

		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => count($res),
			'data'  => ['paper_data'=>$paper_data,'exam'=>$res]
		];
		return response()->json($data);
	}



	// 考试记录列表（数据从前端录入） 考试记录查询 学员ID，考试开始时间-结束时间，交卷开始时间，结束时间
	public function paper_record(Request $request){
		if($request['uid']){
			$where[] = ['uid','=',$request['uid']];
		}
		if($request['created_at_begin'] && $request['created_at_end']){
			$where[] = ['created_at','between',[strtotime($request['created_at_begin']),strtotime($request['created_at_end'])]];
		}
		if($request['submit_at_begin'] && $request['submit_at_end']){
			$where[] = ['submit_at','between',[strtotime($request['submit_at_begin']),strtotime($request['submit_at_end'])]];
		}
		if(!empty($where)){
			$query=PaperRecord::where('pid',$request['pid'])->where($where)->orderBy('id','desc');
		}else{
			$query=PaperRecord::where('pid',$request['pid'])->orderBy('id','desc');
		}
		

		$res=$query->paginate($request->get('limit',10))->toArray();

		
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => $res['total'],
			'data'  => $res['data']
		];
		return response()->json($data);

	}

	public function paper_record_detail(Request $request){
		$res = PaperRecordQuestions::where('record_id',$request['id'])->orderBy('id','desc')->get();

		foreach($res as &$v){
			$v['questions'] = Exam::where('id',$v['exam_id'])->first();
		}
		$paper_data = Paper::where('id',$res[0]['paper_id'])->first();
		$user_data = Users::where('id',$res[0]['user_id'])->select('id','name','phone')->first();
		$data = [
			'code' => 0,
			'msg'   => '请求成功',
			'count' => count($res),
			'data'  => ['paper_data'=>$paper_data,'user_data'=>$user_data,'questions_data'=>$res],
		];
		return response()->json($data);

	}


	
}
